#!/usr/bin/env python
# -*- coding: gb2312 -*-

import cmd

class X(cmd.Cmd):
    src = ""
    disline = 5
    def precmd(self, line):
        if "+" in line[0:1]:
            return "add " + line[1:]
        return line
    def do_s(self, pattern):
        if pattern=="": pattern=X.src;
        print pattern
        str_size = struct.calcsize(pattern)
        oldpos = f21.tell()
        ss = f21.read(str_size)
        f21.seek(oldpos)
        print struct.unpack(pattern, ss)
        X.src = pattern
        pCurdata(f21, str_size/16 + 1)
    def do_add(self, num = 0):
        try:
            if num != 0:
                num=num.strip()
                if num.startswith("0x") or num.startswith("0X"):
                    num = int(num, 16)
                elif num.startswith("0"):
                    num = int(num, 8)
                else :
                    num = int(num)
            if num<0: num = 0
        except:
            num = 0
            print "argument error. auto seek to pos: 0"
        f21.seek(num)
    def do_pr(self, rest):
        if rest =="":
            rest = X.disline
        else:
            try:
                rest = int(rest)
            except:
                rest = disline
        pCurdata(f21, rest)
        X.disline = rest
    def help_pr(self):
        print "print (any string): outputs (any string)"
    def do_stop(self, rest):
        raise StopMe
    def help_stop(self):
        print "stop: terminates the command loop"
    def kki(self):
        pass
